package com.zxx.casserver.base.exception;

import com.zxx.cascommon.base.exception.*;
import com.zxx.cascommon.base.result.Result;
import com.zxx.cascommon.base.result.ResultUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局捕获异常类
 *
 * @author zhoukai
 * @date 2020/3/12 20:37
 */
@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(RateLimiterException.class)
    @ResponseBody
    public Result rateLimiterExceptionHandler(RateLimiterException exception) {
        return ResultUtils.rateLimiterError(exception.getMessage());
    }

    @ExceptionHandler(TokenErrorException.class)
    @ResponseBody
    public Result tokenErrorExceptionHandler(TokenErrorException exception) {
        return ResultUtils.tokenError(exception.getMessage());
    }

    @ExceptionHandler(TokenExpiredException.class)
    @ResponseBody
    public Result tokenExpiredExceptionHandler(TokenExpiredException exception) {
        return ResultUtils.tokenExpired(exception.getMessage());
    }

    @ExceptionHandler(ParamsException.class)
    @ResponseBody
    public Result paramsExceptionHandler(ParamsException exception) {
        return ResultUtils.paramsError(exception.getMessage());
    }

    @ExceptionHandler(InternalErrorException.class)
    @ResponseBody
    public Result internalErrorExceptionHandler(InternalErrorException exception) {
        return ResultUtils.internalError(exception.getMessage());
    }

    @ExceptionHandler(DataNoChangeException.class)
    @ResponseBody
    public Result dataNoChangeExceptionHandler(DataNoChangeException exception) {
        return ResultUtils.noChange(exception.getMessage());
    }

    @ExceptionHandler(DataAlreadyExistException.class)
    @ResponseBody
    public Result dataAlreadyExistExceptionHandler(DataAlreadyExistException exception) {
        return ResultUtils.dataAlreadyExist(exception.getMessage());
    }

    @ExceptionHandler(DataNotExistException.class)
    @ResponseBody
    public Result dataNotExistExceptionHandler(DataNotExistException exception) {
        return ResultUtils.dataNotExit(exception.getMessage());
    }

    @ExceptionHandler(PasswordErrorException.class)
    @ResponseBody
    public Result passwordErrorExceptionHandler(DataNotExistException exception) {
        return ResultUtils.passwordError(exception.getMessage());
    }

}
